//
//  RHPlantTopoHEBATView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/5/20.
//

#import "RHPlantTopoHEBATView.h"
#import "RHBatteryView.h"
#import "YYAnimatedImageView.h"
#import "YYImage.h"

@interface RHPlantTopoHEBATView()
@property (nonatomic ,strong) RHPlantDetailModel *topoModel;

@property (nonatomic ,strong) RHBatteryView *batteryView;

@property (nonatomic ,strong) QMUILabel *socLab;

@property (nonatomic, strong) UIImageView *topoImg;

@property (nonatomic ,strong) UIView *feed_indicator;

@property (nonatomic ,strong) UIView *battery_indicator;

@property (nonatomic ,strong) UIView *power_indicator;

@property (nonatomic ,strong) UIView *load_indicator;

@property (nonatomic ,strong) UIView *solar_indicator;

@property (nonatomic, strong) YYAnimatedImageView *loadGif;

@property (nonatomic, strong) YYAnimatedImageView *solarGif;

@property (nonatomic, strong) YYAnimatedImageView *gridGif;

@property (nonatomic ,assign) BOOL isLandscape;
@end

@implementation RHPlantTopoHEBATView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithOrientation:(BOOL)isLandscape
{
    if(self = [self initWithFrame:CGRectZero]){
        self.backgroundColor = [UIColor whiteColor];
        self.isLandscape = isLandscape;
        [self setupUI];
    }
    return self;
}

//只有电池才除以1000
-(void)setTopoModel:(RHPlantDetailModel *)topoModel deviceTypeCode:(NSString*)deviceTypeCode
{
    _topoModel = topoModel;
    _gridGif.image = nil;
    _loadGif.image = nil;
    _loadGif.image = nil;
    
    //光伏功率
    QMUILabel *solarLab = [self.solar_indicator viewWithTag:100];
    NSString *solar_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.pv doubleValue]]];
    solarLab.text = solar_content;
   
    
    //电网功率
    QMUILabel *gridLab = [self.feed_indicator viewWithTag:100];
    NSString *grid_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.grid doubleValue]]];
    gridLab.text = grid_content;
    
    
    //电池功率
    QMUILabel *batteryLab = [self.battery_indicator viewWithTag:100];
    NSString *battery_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.battery doubleValue]]];
    batteryLab.text = battery_content;
    
    
    //负载功率
    QMUILabel *loadLab = [self.load_indicator viewWithTag:100];
    NSString *load_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.load doubleValue]]];
    loadLab.text = load_content;
    
    
    self.batteryView.soc = topoModel.soc /100.0;
    NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithDouble:topoModel.soc];
    self.socLab.text = [NSString stringWithFormat:@"%@%%",value];
    
    
    //电网动画  (电池功率为正时)
    if ([topoModel.grid floatValue]>0) {
        _gridGif.image = [YYImage imageNamed:@"topo_grid_battery.gif"];
    }
    if ([topoModel.grid floatValue]<0) {
        _gridGif.image = [YYImage imageNamed:@"topo_battery_grid.gif"];
    }
    
    //光伏动画
    if([topoModel.pv floatValue] > 0){
        _solarGif.image = [YYImage imageNamed:@"topo_solar_load.gif"];
    }
    
    //负载动画  (电池功率为负时)
    if([topoModel.load floatValue]>0){
        _loadGif.image = [YYImage imageNamed:@"topo_battery_load.gif"];
    }
    

}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

- (void)setupUI{
   
    kWeakSelf;
    
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
    //全屏
    QMUIButton *fullBtn = [[QMUIButton alloc]init];
    fullBtn.hidden = self.isLandscape;
    [fullBtn setImage:[UIImage imageNamed:@"kwt_full_screen"] forState:UIControlStateNormal];
    [bgView addSubview:fullBtn];
    [fullBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(35);
        make.top.mas_offset(5);
        make.right.mas_offset(-5);
    }];
    
    [[fullBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.fullScreenAction){
            weakSelf.fullScreenAction();
        }
    }];
    
    _topoImg = [[UIImageView alloc] init];
    [bgView addSubview:_topoImg];
    
    _loadGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_loadGif];
    [_loadGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    _solarGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_solarGif];
    [_solarGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    _gridGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_gridGif];
    [_gridGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    //电量
    if (self.isLandscape) {
        UIView *batBgView = kView;
        [bgView addSubview:batBgView];
        [batBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_offset(0);
            make.right.equalTo(weakSelf.topoImg.mas_left).offset(-50);
        }];
    
        //电量
        _batteryView = [[RHBatteryView alloc]init];
        _batteryView.isVertical = YES;
        [batBgView addSubview:_batteryView];
        [_batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.centerY.mas_offset(0);
           make.size.equalTo(@(CGSizeMake(39.5, 72.5)));
        }];
        
        _socLab = [[QMUILabel alloc]init];
        _socLab.text = @"--%";
        _socLab.textColor = kCOLOR_HEX(@"64D941");
        _socLab.font = kSYSTEMFONT(12);
        [bgView addSubview:_socLab];
        [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.batteryView.mas_bottom).offset(10);
            make.centerX.equalTo(weakSelf.batteryView);
        }];
    }else{
        _batteryView = [[RHBatteryView alloc]init];
        [bgView addSubview:_batteryView];
        [_batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_offset(15);
            make.size.equalTo(@(CGSizeMake(33, 18)));
        }];
        _socLab = [[QMUILabel alloc]init];
        _socLab.text = @"--%";
        _socLab.textColor = kCOLOR_HEX(@"64D941");
        _socLab.font = kSYSTEMFONT(12);
        [bgView addSubview:_socLab];
        [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.batteryView.mas_right).offset(10);
            make.centerY.equalTo(weakSelf.batteryView);
        }];
    }
    
    
    
    //馈网
    _feed_indicator = [self indicatorViewWithTitle:@"电网".rh_localized unit:@"kW" volContent:@"V:--V/--V" Alignment:NSTextAlignmentCenter];
    [bgView addSubview:_feed_indicator];
    
    
    //电池
    _battery_indicator = [self indicatorViewWithTitle:@"电池".rh_localized unit:@"kW" volContent:@"V:--V" Alignment:NSTextAlignmentCenter];
    [bgView addSubview:_battery_indicator];
    
    
    //能量
    _power_indicator = [self indicatorViewWithTitle:@"能量".rh_localized unit:@"kW" volContent:@"V:--V/--V" Alignment:NSTextAlignmentRight];
    [bgView addSubview:_power_indicator];
    
    
    //负载
    _load_indicator = [self indicatorViewWithTitle:@"负载".rh_localized unit:@"kW" volContent:@"V:--V/--V" Alignment:NSTextAlignmentLeft];
    [bgView addSubview:_load_indicator];
    
    
    //光伏
    _solar_indicator = [self indicatorViewWithTitle:@"光伏".rh_localized unit:@"kW" volContent:@"V:--V/--V" Alignment:NSTextAlignmentCenter];
    [bgView addSubview:_solar_indicator];
    
    
    UIImage *image = [UIImage imageNamed:@"local_topo_hebat"];
    [_topoImg setImage:image];
    if (self.isLandscape) {
        [_topoImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(30);
            make.bottom.mas_offset(-35);
            make.width.equalTo(weakSelf.topoImg.mas_height).multipliedBy(image.size.width/image.size.height);
            make.centerX.mas_offset(0);
        }];
    }else{
        [_topoImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(fullBtn.mas_bottom).offset(10);
            make.left.mas_offset(25);
            make.right.mas_offset(-25);
            make.bottom.mas_offset(-55);
            make.height.equalTo(weakSelf.topoImg.mas_width).multipliedBy(image.size.height/image.size.width);
        }];
    }
    
    
    //馈网
    [_feed_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topoImg).offset(kScaleW(0));
        make.centerX.equalTo(weakSelf.topoImg.mas_left).offset(30);
    }];
    
    //电池
    [_battery_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(68));
        make.top.equalTo(weakSelf.topoImg.mas_bottom).offset(kScaleW(5));
    }];
    
    
    //负载
    [_load_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topoImg.mas_centerX).offset(kScaleW(35));
        make.centerY.equalTo(weakSelf.topoImg.mas_bottom).offset(-kScaleW(0));
    }];
    
    //光伏
    [_solar_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(kScaleW(5));
        make.top.equalTo(weakSelf.topoImg).offset(kScaleW(-10));
    }];
    
    //能量
    [_power_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.battery_indicator);
        make.width.offset(120);
        make.bottom.equalTo(weakSelf.feed_indicator.mas_top).offset(-kScaleW(20));
    }];
    _power_indicator.hidden = YES;
    
}



//家储示意图 (能量,馈网,电池,光伏,负载)
-(UIView*)indicatorViewWithTitle:(NSString*)title unit:(NSString*)unitStr volContent:(NSString*)volContent Alignment:(NSTextAlignment)alignment;
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.clearColor;
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = alignment;
    titleLab.numberOfLines = 0;
    titleLab.text = title;
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    titleLab.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:titleLab];
    
    //功率
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = alignment;
    contentLab.text = [NSString stringWithFormat:@"--%@",unitStr];
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = kSYSTEMFONT(10);
    contentLab.tag = 100;
    [bgView addSubview:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.mas_offset(0);
    }];

    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.equalTo(contentLab.mas_bottom).offset(5);
        make.bottom.mas_offset(0);
        
    }];
    
    return bgView;
}

@end
